<template>
	<el-drawer v-if="isShowSetDrawer" v-model="isShowSetDrawer" :with-header="false" :size="curDrawerSize" :close-on-click-modal="false" class="zs-drawer-custom zs-drawer-transparent">
		<iframe class="zs-iframe" ref="iframeWin" @load="iframeLoading" :open-animation="false" :src="`${iframeUrl}?taskId=${selectItem.triggerTaskId}&isReadOnly=${planReadonly}&tokenJson=${tokenJson}`"> </iframe>
	</el-drawer>
</template>

<script setup>
	import { ref } from 'vue';
	import { useStore } from 'vuex';
	import { ElLoading } from 'element-plus';

	const store = useStore();
	const curDrawerSize = ref(store.state.uiDrawerSize);
	const emit = defineEmits(['queryList']);

	let selectItem = ref({});
	const isShowSetDrawer = ref(false);
	const planReadonly = ref(false);
	const listenMsg = event => {
		const data = event.data.data;
		if (data && data.TaskSchedulingSetPlanIframeIsShow == false) {
			isShowSetDrawer.value = data.TaskSchedulingSetPlanIframeIsShow;
			emit('queryList');
			window.removeEventListener('message', listenMsg, true);
		}
	};
	// let loading = ref(false);
	let loading = null;
	const setRow = (row, isPlanReadonly) => {
		// loading.value = true;
		loading = ElLoading.service({
			lock: true,
			background: 'rgba(255, 255, 255, 0)',
		});
		selectItem.value = row;
		planReadonly.value = isPlanReadonly;
		isShowSetDrawer.value = true;
		window.addEventListener('message', listenMsg, true);
	};
	const iframeLoading = () => {
		loading.close();
		// loading.value = false;
	};

	const tokenJson = localStorage.getItem('authToken');
	let iframeUrl = ref('');
	if (window.location.href.indexOf('portal-uat.crehopson.com') != -1) {
		iframeUrl.value = 'https://hs-approval-pc-uat.crehopson.com/approval-pc/TaskSchedulingSetPlanIframe';
	} else if (window.location.href.indexOf('wutong.crehopson.com') != -1) {
		iframeUrl.value = 'https://hs-approval-pc.crehopson.com/approval-pc/TaskSchedulingSetPlanIframe';
	} else {
		iframeUrl.value = 'http://localhost:8092/approval-pc/TaskSchedulingSetPlanIframe';
	}

	defineExpose({ setRow });
</script>
